package ncut.shumei.controller;

import io.swagger.annotations.*;
import ncut.shumei.dto.ElevatorCreateRequest;
import ncut.shumei.dto.ElevatorDTO;
import ncut.shumei.service.ElevatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/api/elevators")
@Api(tags = "电梯管理")
public class ElevatorController {

    @Autowired
    private ElevatorService elevatorService;

    @ApiOperation("获取电梯列表")
    @GetMapping
    public ResponseEntity<List<ElevatorDTO>> getElevators(
            @ApiParam("建筑物名称") @RequestParam(required = false) String building,
            @ApiParam("电梯状态") @RequestParam(required = false) String status) {
        return ResponseEntity.ok(elevatorService.getElevators(building, status));
    }

    @ApiOperation("获取电梯详情")
    @GetMapping("/{id}")
    public ResponseEntity<ElevatorDTO> getElevator(@PathVariable Integer id) {
        return ResponseEntity.ok(elevatorService.getElevator(id));
    }

    @ApiOperation("创建电梯")
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ElevatorDTO> createElevator(@RequestBody ElevatorCreateRequest request) {
        return ResponseEntity.ok(elevatorService.createElevator(request));
    }

    @ApiOperation("更新电梯")
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ElevatorDTO> updateElevator(
            @PathVariable Integer id,
            @RequestBody ElevatorCreateRequest request) {
        return ResponseEntity.ok(elevatorService.updateElevator(id, request));
    }

    @ApiOperation("删除电梯")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<Void> deleteElevator(@PathVariable Integer id) {
        elevatorService.deleteElevator(id);
        return ResponseEntity.ok().build();
    }

    @ApiOperation("更新电梯状态")
    @PutMapping("/{id}/status")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<Void> updateStatus(
            @PathVariable Integer id,
            @RequestParam String status) {
        elevatorService.updateElevatorStatus(id, status);
        return ResponseEntity.ok().build();
    }
} 